import numpy as np
import csv
import random
from datetime import datetime
from time import strftime
from dbfpy import dbf

'''
read dbf file
'''

def getCurTime():
    """
    get current time
    Return value of the date string format(%Y-%m-%d %H:%M:%S)
    """
    format='%Y-%m-%d %H:%M:%S'
    sdate = None
    cdate = datetime.now()
    try:
        sdate = cdate.strftime(format)
    except:
        raise ValueError
    return sdate

def build_data_list(inputCSV):
    sKey = []
    fn = inputCSV
    ra = csv.DictReader(file(fn), dialect="excel")
    sumPop = 0
    sumCases = 0
    for record in ra:
        #print record[ra.fieldnames[0]], type(record[ra.fieldnames[-1]])
        for item in ra.fieldnames:
            #temp = record[item]
            sKey.append(record[item])
        sumCases += float(record[ra.fieldnames[-1]])
        sumPop += float(record[ra.fieldnames[-4]])
        sKey.append('0')
        sKey.append('0')
        sKey.append('0')
        sKey.append('0')
        sKey.append('0')
    sKey = np.array(sKey)
    sKey.shape=(-1,len(ra.fieldnames) + 5)
    return sKey, sumCases, sumPop


def calCI(cases, pop, sumCases, sumPop):
    cases = float(cases)
    pop = float(pop)
    expectedC = (pop + 0.0)*sumCases/sumPop
    temp_smr = (cases + 0.0)/expectedC
    temp = 1.96 * (cases ** 0.5)/expectedC
    return expectedC, temp_smr, temp_smr - temp, temp_smr + temp

def calCIuseExpected(cases, expected):
    temp_smr = (cases + 0.0)/expected
    temp = 1.96 * (cases ** 0.5)/expected
    return temp_smr - temp, temp_smr + temp

#--------------------------------------------------------------------------
#MAIN
if __name__ == "__main__":
    print "begin at " + getCurTime()
    filePath = 'C:/_DATA/migration89_08/COUNTY Migration/SaTScan/exclude_nearest/'
    inputCSV = filePath + 'us_conty_reducedInflow_1.csv'
    data, sumCases, sumPop = build_data_list(inputCSV)
    #print calCIuseExpected(12357039, 7928835)

    
    for item in data:
        tExpected, t_smr, tLLeft, tLRight = calCI(item[6], item[3], sumCases, sumPop)
        item[7] = str(int(tExpected))
        item[8] = str(t_smr)
        item[9] = str(tLLeft)
        item[10] = str(tLRight)
        if tLLeft > 1:
            item[11] = 'Y'
        else:
            item[11] = 'N'

    fileLoc = filePath + 'us_conty_reducedInflow_smr.csv'
    np.savetxt(fileLoc, data, delimiter=',', fmt = '%s')
    
    print "end at " + getCurTime()
    print "========================================================================"  
    
            